package com.yyyzzz.servlet;

import com.mysql.cj.Session;
import com.yyyzzz.DAO.impl.teacherDaoimpl;
import com.yyyzzz.bean.Teacher;
import com.yyyzzz.service.impl.teacherServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

public class teacherLoginServlet extends HttpServlet {
    @Override
    protected void doGet (HttpServletRequest req , HttpServletResponse resp) throws ServletException, IOException {
int id = Integer.parseInt (req.getParameter ("teacherNo"));
String pwd = req.getParameter ("teacherpassword");
        try {
            teacherDaoimpl teacherDaoimpl = new teacherDaoimpl();
            Teacher teacher = teacherDaoimpl.getTeacherById (id);
            if ( teacher==null ){
                resp.getWriter ().write ("您尚未注册次教师号，请前去注册把！！！");
            }
            if ( teacher.getTeacherpassword ().equals (pwd) ){
                resp.getWriter ().write ("恭喜您！用户名为:"+teacher.getTeachername ()+"登陆成功！！！");
                HttpSession session = req.getSession ();
                session.setAttribute ("id",teacher.getTeacherNo ());
                session.setAttribute ("teacher",teacher);
                resp.sendRedirect ("teacherMain.jsp");
            }else {
                resp.getWriter ().write ("用户名或密码不正确，请重新输入！！！");
            }
        } catch (ClassNotFoundException e) {
            System.out.println ("1");
            e.printStackTrace ();
        } catch (SQLException throwables) {
            System.out.println ("2");
            throwables.printStackTrace ();
        }
    }

    @Override
    protected void doPost (HttpServletRequest req , HttpServletResponse resp) throws ServletException, IOException {
        doGet (req , resp);
    }
}
